package com.example.nacosdemo.controller;

import cn.highsuccess.connPool.api.tssc.UnionTsscResult;
import com.example.nacosdemo.config.ParamConfig;
import com.example.nacosdemo.vo.StorePikVo;
import fpsp.hsm.client.UnionTssc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/union")
public class UnionTsscController {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UnionTssc unionTssc;

    @Autowired
    private ParamConfig config;

    /**
     * 生成Mac校验
     *
     * @param macString
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/generateMac", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hsmGenerateMac(@RequestBody String macString) throws Exception {
        log.info("generateMac->{}", macString);
        UnionTsscResult Text1;
        try {
            Text1 = unionTssc.hsm_generate_mac(config.getBrhID(), macString.length() + "", macString.getBytes());
        } catch (Exception e) {
            log.error("系统异常", e);
            return e.getMessage();
        }
        log.info("generateMac->{}", macString);
        return Text1.getStr();
    }

    /**
     * 生成Mac校验
     *
     * @param vo
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/hsmStorePik", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hsmStorePik(@RequestBody StorePikVo vo) throws Exception {
        log.info("hsmStorePik->{}", vo.toString());
        try {
            unionTssc.hsm_store_pik(config.getBrhID(), vo.getMacKey(), vo.getMacCKV());
        } catch (Exception e) {
            log.error("系统异常", e);
            return e.getMessage();
        }
        log.info("hsmStorePik->OK");
        return "ok";
    }

}
